<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="auto">
            <el-row>
                <el-form-item label="Date" label-width="auto">
                    <el-col :span="4">
                        <el-date-picker
                            v-model="daterangeDate"
                            style="width:220px"
                            value-format="yyyy-MM-dd"
                            type="daterange"
                            range-separator="-"
                            start-placeholder="Start Date"
                            end-placeholder="End Date"
                        ></el-date-picker>
                    </el-col>
                </el-form-item>
                <el-form-item label="EmployeeID" prop="employeeId" label-width="auto" v-has-permi="['business:innovation:searchAll']">
                    <el-col :span="4">
                        <el-input style="width:150px"
                                  v-model="queryParams.employeeId"
                                  placeholder="Input EmployeeID"
                                  clearable
                                  @keyup.enter.native="handleQuery"
                        />
                    </el-col>
                </el-form-item>
                <el-form-item label="Name" prop="employeeName" label-width="auto"  v-has-permi="['business:innovation:searchAll']">
                    <el-col :span="4">
                        <el-input style="width:150px"
                                  v-model="queryParams.employeeName"
                                  placeholder="Input Name"
                                  clearable
                                  @keyup.enter.native="handleQuery"
                        />
                    </el-col>
                </el-form-item>
                <el-form-item label="Dept." prop="deptId" label-width="auto"  v-has-permi="['business:innovation:searchAll']">
                    <el-col :span="4">
                        <el-select v-model="queryParams.deptId" style="width:150px" placeholder="Select Dept." clearable>
                            <el-option
                                v-for="item in deptList "
                                :key="item.deptId"
                                :label="item.deptName"
                                :value="item.deptId"
                            />
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label="Status" prop="status">
                    <el-col :span="4">
                        <el-select v-model="queryParams.status" style="width:150px" placeholder="Select Status" clearable>
                            <el-option
                                v-for="dict in dict.type.business_status"
                                :key="dict.value"
                                :label="dict.label"
                                :value="dict.value"
                            />
                        </el-select>
                    </el-col>
                </el-form-item>
                <el-form-item label-width="auto">
                    <el-col :span="4">
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search
                        </el-button>
                    </el-col>
                </el-form-item>
                <el-form-item label-width="auto">
                    <el-col :span="4">
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">Reset</el-button>
                    </el-col>
                </el-form-item>
            </el-row>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['business:innovation:add']"
                >Add
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['business:innovation:remove']"
                >Delete
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['business:innovation:export']"
                >Export
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="innovationList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
                        <el-table-column
                            label="No."
                            type="index"
                            width="50"/>
            <el-table-column label="No." align="center" prop="id" v-if="false"/>
            <el-table-column label="Date" align="center" prop="date" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="Dept." align="center" prop="deptId" :formatter="deptFormatter"/>
            <el-table-column label="EmployeeID" align="center" prop="employeeId"/>
            <el-table-column label="Name" align="center" prop="employeeName"/>
            <el-table-column label="Title" align="center" prop="title"/>
            <el-table-column label="Result" align="center" prop="result" :show-overflow-tooltip="true"/>
            <el-table-column label="DueDate" align="center" prop="dueDate" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.dueDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="ClosingDate" align="center" prop="closingDate" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.closingDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="Status" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.business_status" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="Remark" align="center" prop="remark" :show-overflow-tooltip="true"/>
            <el-table-column label="Attachment" align="center" prop="attachment">
                <template v-slot="scope">
                    <el-popover
                        placement="right"
                        width="400"
                        trigger="click">
                        <span v-if="showFileList(scope.row).len > 0">
                        <el-link target="_blank"
                                 v-for="fileUrl in showFileList(scope.row).data" type="primary"
                                 :href="fileUrl">{{ fileUrl.slice(fileUrl.lastIndexOf("/") + 1) }}
                        </el-link>
                            </span>
                        <span v-else>No File</span>
                        <el-link slot="reference" type="primary">File Query</el-link>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="Operate" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        type="warning"
                        size="small"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:innovation:edit']"
                    >Edit
                    </el-button>
                    <!--                    <el-button-->
                    <!--                        size="mini"-->
                    <!--                        type="text"-->
                    <!--                        icon="el-icon-delete"-->
                    <!--                        @click="handleDelete(scope.row)"-->
                    <!--                        v-hasPermi="['business:innovation:remove']"-->
                    <!--                    >Delete-->
                    <!--                    </el-button>-->
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改innovation对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="50%" :append-to-body="true" :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="auto" >
                <el-row>
                    <el-col :span="5">
                        <el-form-item label="EmployeeID" prop="employeeId">
                            <el-input type="" v-model="form.employeeId" placeholder="Input EmployeeID" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="Name" prop="employeeName">
                            <el-input v-model="form.employeeName" placeholder="Input Name" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="Dept." prop="DeptId">
                            <el-select v-model="form.deptId" placeholder="Dept." disabled>
                                <el-option
                                    v-for="item in deptList"
                                    :key="item.deptId"
                                    :label="item.deptName"
                                    :value="parseInt(item.deptId)">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                        <el-col :span="7">
                            <el-form-item label="Date" prop="date">
                                <el-date-picker clearable
                                                readonly
                                                style="width: 100%"
                                                v-model="form.date"
                                                type="date"
                                                value-format="yyyy-MM-dd"
                                                placeholder="Select Date">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                </el-row>
                <el-form-item label="Title" prop="title">
                    <el-input v-model="form.title" placeholder="Input Title"/>
                </el-form-item>
                <el-form-item label="Result" prop="result">
                    <el-input
                         :disabled="!$auth.hasPermi('business:innovation:result')" v-model="form.result" type="textarea" :rows="4"/>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="DueDate" prop="dueDate">
                            <el-date-picker clearable
                                            v-model="form.dueDate"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="DueDate">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col  :span="12">
                        <el-form-item label="ClosingDate" prop="closingDate">
                            <el-date-picker clearable
                                            v-model="form.closingDate"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="ClosingDate">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="Status" prop="status">
                    <el-select v-model="form.status" placeholder="Status">
                        <el-option
                            v-for="dict in dict.type.business_status"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="RemarK" prop="remark">
                    <el-input v-model="form.remark" :rows="4" type="textarea" placeholder="Remark"/>
                </el-form-item>
                <el-form-item label="Attachment" prop="attachment">
                    <FileUpload v-model="form.attachment"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">Submit</el-button>
                <el-button @click="cancel">Cancel</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listInnovation,
    getInnovation,
    delInnovation,
    addInnovation,
    updateInnovation,
    getDeptList
} from "@/api/business/innovation";

export default {
    name: "Innovation",
    dicts: ['business_status'],
    data() {
        return {
            user: {},
            deptList: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // innovation表格数据
            innovationList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // Attachment时间范围
            daterangeDate: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                date: null,
                deptId: null,
                employeeId: null,
                employeeName: null,
                title: null,
                result: null,
                status: null,
                attachment: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                title: [
                    {required: true, message: "Title Not Null", trigger: "blur"}
                ],
                dueDate: [
                    {required: true, message: "DueDate Not Null", trigger: "blur"}
                ],
                closingDate: [
                    {required: true, message: "ClosingDate Not Null", trigger: "blur"}
                ],
                status: [
                    {required: true, message: "Status Not Null", trigger: "change"}
                ],
                remark: [
                    {required: true, message: "RemarK Not Null", trigger: "blur"}
                ],
            }
        };
    },
    created() {
        this.user = this.$store.state.user;
        console.log(this.user)
        this.getDeptList();
        this.getList();
    },
    methods: {
        showFileList(row) {
            let ls = [];
            let str = row.attachment
            let len = 0;
            if (str) {
                str.split(",").forEach(i => {
                    len = len + 1
                    ls.push(process.env.VUE_APP_BASE_API + i);
                });
            }
            return {'data': ls, 'len': len}
        },
        /** 查询innovation列表 */
        getList() {
            this.loading = true;
            this.queryParams.params = {};
            let searchAll = this.$auth.hasPermi("business:innovation:searchAll")
            if (!searchAll){
                this.queryParams.employeeId= this.user.employeeId
            }
            if (null != this.daterangeDate && '' != this.daterangeDate) {
                this.queryParams.params["beginDate"] = this.daterangeDate[0];
                this.queryParams.params["endDate"] = this.daterangeDate[1];
            }
            listInnovation(this.queryParams).then(response => {
                console.log(this.queryParams)
                this.innovationList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        getDeptList() {
            getDeptList().then(res => {
                this.deptList = res.data
            })
        },
        deptFormatter(row) {
            let res = this.deptList.find(i => {
                return row.deptId == i.deptId
            });
            return res && res.deptName;
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                date: null,
                deptId: null,
                employeeId: null,
                employeeName: null,
                title: null,
                result: null,
                dueDate: null,
                closingDate: null,
                status: null,
                remark: null,
                attachment: null
            };
            this.resetForm("form");
            this.fileList = []
        },
        // 文件上传
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${file.name}？`);
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.daterangeDate = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "Add Innovation";
            this.form.employeeId = this.user.employeeId
            this.form.employeeName = this.user.name
            this.form.deptId = this.user.deptId
            this.form.date = new Date()
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getInnovation(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "Edit Innovation";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateInnovation(this.form).then(response => {
                            this.$modal.msgSuccess("Edit Success");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addInnovation(this.form).then(response => {
                            this.$modal.msgSuccess("Add Success");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('Delete Innovation "' + ids + '"？').then(function () {
                return delInnovation(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("Delete Success");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/innovation/export', {
                ...this.queryParams
            }, `innovation_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
